/*     */ package com.newcrm.db;
/*     */ 
/*     */ import com.newcrm.exception.DBException;
/*     */ import java.sql.Connection;
/*     */ import java.sql.SQLException;
/*     */ import java.util.Date;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.apache.commons.dbutils.DbUtils;
/*     */ import org.apache.commons.dbutils.GenKeyQueryRunner;
/*     */ import org.apache.commons.dbutils.MyMapListHandler;
/*     */ import org.apache.commons.dbutils.QueryRunner;
/*     */ import org.apache.commons.dbutils.handlers.MapHandler;
/*     */ import org.apache.commons.dbutils.handlers.ScalarHandler;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class CompanyManager
/*     */ {
/*  22 */   private static Logger logger = Logger.getLogger(CompanyManager.class
/*  23 */     .getName());
/*     */ 
/*     */   public int add(String companyName)
/*     */     throws DBException, SQLException
/*     */   {
/*  34 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  35 */     if (conn == null) {
/*  36 */       throw new DBException();
/*     */     }
/*  38 */     GenKeyQueryRunner runner = new GenKeyQueryRunner(
/*  39 */       new ScalarHandler(), new String[] { "CompanyId" });
/*     */     try {
/*  41 */       runner.update(conn, "INSERT INTO T_Company_Info (CompanyName, IsValid, LicenceNum, CreateDate, UpdateDate) VALUES (?, 1, 0, NOW(), NOW())", companyName);
/*  42 */       int companyId = ((Long)runner.getGeneratedKeys()).intValue();
/*  43 */       return companyId;
/*     */     } finally {
/*     */       try {
/*  46 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/*  48 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public int add(String companyName, int isValid, int licenceNum, String category)
/*     */     throws DBException, SQLException
/*     */   {
/*  66 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  67 */     if (conn == null) {
/*  68 */       throw new DBException();
/*     */     }
/*  70 */     GenKeyQueryRunner runner = new GenKeyQueryRunner(
/*  71 */       new ScalarHandler(), new String[] { "CompanyId" });
/*     */     try {
/*  73 */       runner.update(conn, "INSERT INTO T_Company_Info (CompanyName, IsValid, LicenceNum, Category, CreateDate, UpdateDate) VALUES (?, ?, ?, ?, NOW(), NOW())", new Object[] { companyName, 
/*  74 */         Integer.valueOf(isValid), Integer.valueOf(licenceNum), category });
/*  75 */       int companyId = ((Long)runner.getGeneratedKeys()).intValue();
/*     */ 
/*  77 */       return companyId;
/*     */     } finally {
/*     */       try {
/*  80 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/*  82 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public String getCompanyName(int companyId)
/*     */   {
/*  94 */     if (companyId <= 0) {
/*  95 */       return null;
/*     */     }
/*  97 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  98 */     if (conn == null) {
/*  99 */       return null;
/*     */     }
/* 101 */     QueryRunner runner = new QueryRunner();
/*     */     try
/*     */     {
/* 104 */       Map result = (Map)runner.query(conn, 
/* 105 */         "SELECT CompanyName FROM T_Company_Info WHERE CompanyId = ?", new MapHandler(), new Object[] { Integer.valueOf(companyId) });
/*     */ 
/* 107 */       if ((result == null) || (result.size() == 0)) {
/* 108 */         return null;
/*     */       }
/* 110 */       return (String)result.get("CompanyName");
/*     */     } catch (SQLException localSQLException1) {
/*     */     } finally {
/*     */       try {
/* 114 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 116 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/* 119 */     return null;
/*     */   }
/*     */ 
/*     */   public Date getCompanyCreateDate(int companyId)
/*     */   {
/* 129 */     if (companyId <= 0) {
/* 130 */       return null;
/*     */     }
/* 132 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 133 */     if (conn == null) {
/* 134 */       return null;
/*     */     }
/* 136 */     QueryRunner runner = new QueryRunner();
/*     */     try
/*     */     {
/* 139 */       Map result = (Map)runner.query(conn, 
/* 140 */         "SELECT CreateDate FROM T_Company_Info WHERE CompanyId = ?", new MapHandler(), new Object[] { Integer.valueOf(companyId) });
/*     */ 
/* 142 */       if ((result == null) || (result.size() == 0)) {
/* 143 */         return null;
/*     */       }
/*     */ 
/* 146 */       return (Date)result.get("CreateDate");
/*     */     } catch (SQLException localSQLException1) {
/*     */     } finally {
/*     */       try {
/* 150 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 152 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/* 155 */     return null;
/*     */   }
/*     */ 
/*     */   public Integer getCompanyId(String companyName)
/*     */   {
/* 165 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 166 */     if (conn == null) {
/* 167 */       return null;
/*     */     }
/* 169 */     QueryRunner runner = new QueryRunner();
/*     */     try
/*     */     {
/* 172 */       Map result = (Map)runner.query(conn, 
/* 173 */         "SELECT CompanyId FROM T_Company_Info WHERE CompanyName = ?", new MapHandler(), new Object[] { companyName });
/*     */ 
/* 175 */       if ((result == null) || (result.size() == 0)) {
/* 176 */         return null;
/*     */       }
/*     */ 
/* 179 */       return (Integer)result.get("CompanyId");
/*     */     } catch (SQLException localSQLException1) {
/*     */     }
/*     */     finally {
/*     */       try {
/* 184 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 186 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/* 189 */     return null;
/*     */   }
/*     */ 
/*     */   public List<Map<String, Object>> getAllCompanys()
/*     */   {
/* 198 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 199 */     if (conn == null) {
/* 200 */       return null;
/*     */     }
/* 202 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/* 204 */       return (List)runner.query(conn, "SELECT A.*,B.QQ,B.Mobile,B.UserId,B.Name,B.Email FROM T_Company_Info AS A INNER JOIN T_User_Info AS B ON A.CompanyId = B.CompanyId AND B.IsCreater = 1 ORDER BY A.CreateDate DESC", 
/* 205 */         new MyMapListHandler());
/*     */     } catch (SQLException e) {
/* 207 */       return null;
/*     */     } finally {
/*     */       try {
/* 210 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 212 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public int open(int companyId)
/*     */   {
/* 224 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 225 */     if (conn == null) {
/* 226 */       return 0;
/*     */     }
/* 228 */     QueryRunner runner = new QueryRunner();
/*     */     try
/*     */     {
/* 231 */       return runner.update(conn, "UPDATE T_Company_Info SET IsValid = 1, UpdateDate = NOW() WHERE CompanyId = ?", Integer.valueOf(companyId));
/*     */     } catch (SQLException e) {
/* 233 */       logger.warn("open company failed", e);
/* 234 */       return 0;
/*     */     } finally {
/*     */       try {
/* 237 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 239 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public int close(int companyId)
/*     */   {
/* 251 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 252 */     if (conn == null) {
/* 253 */       return 0;
/*     */     }
/* 255 */     QueryRunner runner = new QueryRunner();
/*     */     try
/*     */     {
/* 258 */       return runner.update(conn, "UPDATE T_Company_Info SET IsValid = 0, UpdateDate = NOW() WHERE CompanyId = ?", Integer.valueOf(companyId));
/*     */     } catch (SQLException e) {
/* 260 */       logger.warn("close company failed", e);
/* 261 */       return 0;
/*     */     } finally {
/*     */       try {
/* 264 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 266 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public int getMaxUsersNum(int companyId)
/*     */   {
/* 278 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 279 */     if (conn == null) {
/* 280 */       return 0;
/*     */     }
/*     */ 
/* 283 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/* 285 */       Object obj = runner.query(conn, "SELECT LicenceNum FROM T_Company_Info WHERE CompanyId = ?", 
/* 286 */         new ScalarHandler(1), new Object[] { Integer.valueOf(companyId) });
/* 287 */       if (obj == null) {
/* 288 */         return 0;
/*     */       }
/* 290 */       if (!(obj instanceof Integer)) {
/* 291 */         return 0;
/*     */       }
/* 293 */       return ((Integer)obj).intValue();
/*     */     } catch (SQLException e) {
/* 295 */       logger.warn("get count failed", e);
/*     */     } finally {
/*     */       try {
/* 298 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 300 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/* 303 */     return 0;
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.db.CompanyManager
 * JD-Core Version:    0.6.1
 */